package com.imooc.controller;

import com.imooc.bo.UserAddressBo;
import com.imooc.pojo.UserAddress;
import com.imooc.service.UserAddressService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.MobileEmailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wangke
 * @version 1.0.0
 * @Description 收货地址控制器，处理收货地址相关
 * @createTime 2021年08月01日 19:29:00
 */

@RestController
@RequestMapping("/address")
@Api(value = "收获地址的controller",tags = "处理收获地址相关请求接口")
public class UserAddressController {
    @Autowired
    private UserAddressService addressService;

    @PostMapping("/list")
    @ApiOperation(value = "获取收货地址",notes="获取当前用户所有收货地址",httpMethod = "POST")
    public IMOOCJSONResult getAllAddress(@RequestParam @ApiParam(name ="userId" ,value = "登录用户的id") String userId){
      if(StringUtils.isBlank(userId)){
          return IMOOCJSONResult.errorMsg("用户id不能为空");
      }
        List<UserAddress> addresses = addressService.queryAllAddress(userId);
        return  IMOOCJSONResult.ok(addresses);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增收货地址信息",notes = "下单新增收货地址",httpMethod = "POST")
   public IMOOCJSONResult addUserAddress(@RequestBody UserAddressBo addressBo){
        IMOOCJSONResult imoocjsonResult = checkAddress(addressBo);
        if(imoocjsonResult.getStatus()!=200){
            return IMOOCJSONResult.errorMsg("收货地址信息不能为空");
        }
        addressService.addUserAddress(addressBo);
        return IMOOCJSONResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除收货地址",notes = "删除用户收货地址")
    public IMOOCJSONResult deleteAddress(@RequestParam String userId,@RequestParam String addressId){
        if(StringUtils.isBlank(userId)||StringUtils.isBlank(addressId)){
            return IMOOCJSONResult.errorMsg("id不能为空");
        }
        addressService.deleteUserAddress(userId,addressId);
        return IMOOCJSONResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改收货地址",notes = "修改用户账户下收货地址")
    public IMOOCJSONResult updateAddress(@RequestBody UserAddressBo addressBo){
        IMOOCJSONResult imoocjsonResult = checkAddress(addressBo);
        if(imoocjsonResult.getStatus()!=200){
            return IMOOCJSONResult.errorMsg("收货信息格式错误");
        }
        addressService.updateAddress(addressBo);
        return  IMOOCJSONResult.ok();
    }

    @PostMapping("/setDefalut")
    public IMOOCJSONResult setDefaultAddress(@RequestParam String userId,@RequestParam String addressId){
        if(StringUtils.isBlank(userId)||StringUtils.isBlank(addressId)){
            return IMOOCJSONResult.errorMsg("id不能为空");
        }
        addressService.setDefalutAddress(userId,addressId);
        return IMOOCJSONResult.ok();

    }



    //校验收货信息
    private IMOOCJSONResult checkAddress(UserAddressBo addressBO) {
        String receiver = addressBO.getReceiver();
        if (StringUtils.isBlank(receiver)) {
            return IMOOCJSONResult.errorMsg("收货人不能为空");
        }
        if (receiver.length() > 12) {
            return IMOOCJSONResult.errorMsg("收货人姓名不能太长");
        }

        String mobile = addressBO.getMobile();
        if (StringUtils.isBlank(mobile)) {
            return IMOOCJSONResult.errorMsg("收货人手机号不能为空");
        }
        if (mobile.length() != 11) {
            return IMOOCJSONResult.errorMsg("收货人手机号长度不正确");
        }
        boolean isMobileOk = MobileEmailUtils.checkMobileIsOk(mobile);
        if (!isMobileOk) {
            return IMOOCJSONResult.errorMsg("收货人手机号格式不正确");
        }

        String province = addressBO.getProvince();
        String city = addressBO.getCity();
        String district = addressBO.getDistrict();
        String detail = addressBO.getDetail();
        if (StringUtils.isBlank(province) ||
                StringUtils.isBlank(city) ||
                StringUtils.isBlank(district) ||
                StringUtils.isBlank(detail)) {
            return IMOOCJSONResult.errorMsg("收货地址信息不能为空");
        }

        return IMOOCJSONResult.ok();
    }


}
